package vn.conghuy.talkwithme.responsor.subresponsor;

import vn.conghuy.talkwithme.MyApp;
import vn.conghuy.talkwithme.R;
import vn.conghuy.talkwithme.data.sqlmodel.SqlData;
import vn.conghuy.talkwithme.responsor.responsor.MapResponsor;
import vn.conghuy.talkwithme.utils.DoubleWrapper;
import vn.conghuy.talkwithme.utils.StringWrapper;
import vn.conghuy.talkwithme.utils.Utils;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;

public class ShowLocationOnMapSubResponsor {
	
	public static final int RELATIVE_NONE = 0;
	public static final int RELATIVE_CONFIRM = 1;
	public static final int RELATIVE_REINPUT_ADDRESS = 2;
	
	MapResponsor mapResponsor;
	MyApp myApp;
	Context context;
	int relativeWithLastResponse;
	String locationName;
	double latitude;
	double longtidude;
	
	
	public ShowLocationOnMapSubResponsor(MapResponsor mapResponsor, Context context) {
		this.mapResponsor = mapResponsor;
		this.context = context;
		myApp = (MyApp)context.getApplicationContext();
	}
	
	public String response(String request){
		
		// example: show me Ho Chi Minh City on the map
		// show me : keyword
		// on: preposition
		// the map: tail
		// request has to have keyword, preposition. Tail is phrase after preposition. Phrase has to have tail keyword defined in database
		switch(relativeWithLastResponse){
		
		case RELATIVE_NONE:
			StringWrapper keywordWrapper = new StringWrapper();
			StringWrapper prepositionWrapper = new StringWrapper();
			StringWrapper tailWrapper = new StringWrapper();
			
			if(Utils.isExistElemInArrayIsWordOfStr(SqlData.mapAskLocationKeyword, request, keywordWrapper)
					&& Utils.isExistElemInArrayIsWordOfStr(SqlData.mapAskLocationPreposition, request, prepositionWrapper)
					&& Utils.isExistElemInArrayIsWordOfStr(SqlData.mapAskLocationTail, request, tailWrapper)){
				
				String keyword = keywordWrapper.getStr();
				int locationIndex = Utils.indexOfWord(request, keyword);
				locationIndex += keyword.length() + 1;
				
				String preposition = prepositionWrapper.getStr();
				int prepositionIndex = Utils.indexOfWord(request, preposition);
				int endLocationIndex = prepositionIndex - 1;
				
				String tailKeyword = tailWrapper.getStr();
				int tailIndex = Utils.indexOfWord(request, tailKeyword);
				
				//if in tail phrase has tail keyword, do
				if(tailIndex > prepositionIndex){
					String locationInput = request.substring(locationIndex, endLocationIndex);
					return parseLocationPhraseAndSetRelative(locationInput);
					
				}
			}
			break;
			
		case RELATIVE_CONFIRM:
			if(Utils.isExistElemInArrayIsWordOfStr(SqlData.callYesWords, request, null)){
				relativeWithLastResponse = RELATIVE_NONE;
				mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_NONE);
				showMap();
				return context.getString(R.string.show_map_command);
			}
			else if(Utils.isExistElemInArrayIsWordOfStr(SqlData.callNoWords, request, null)){
				StringWrapper correctKeywordWrapper = new StringWrapper(); 
				if(Utils.isExistElemInArrayIsWordOfStr(SqlData.correctKeyword, request, correctKeywordWrapper)){
					String correctKeyword = correctKeywordWrapper.getStr();
					int locationPhraseIndex = Utils.indexOfWord(request, correctKeyword) + correctKeyword.length() + 1;
					String locationInput = request.substring(locationPhraseIndex);
					return parseLocationPhraseAndSetRelative(locationInput);
				}
				else{
					relativeWithLastResponse = RELATIVE_REINPUT_ADDRESS;
					mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_SHOW_LOCATION);
					return Utils.getRandomString(SqlData.mapAskReinputAddress);
				}
			}
			else{
				return parseLocationPhraseAndSetRelative(request);
				
			}
			
		case RELATIVE_REINPUT_ADDRESS:
			keywordWrapper = new StringWrapper();
			prepositionWrapper = new StringWrapper();
			String keyword = null;
			String preposition = null;
			
			int keywordIndex = -1, prepositionIndex = -1;
			int locationPhraseIndex = 0;
			int endLocationPhraseIndex = request.length();
			if(Utils.isExistElemInArrayIsWordOfStr(SqlData.mapAskLocationKeyword, request, keywordWrapper)){
				keyword = keywordWrapper.getStr();
				keywordIndex = Utils.indexOfWord(request, keyword);
				locationPhraseIndex = keywordIndex + keyword.length() + 1;
			}
			
			if(Utils.isExistElemInArrayIsWordOfStr(SqlData.mapAskLocationPreposition, request, prepositionWrapper)){
				preposition = prepositionWrapper.getStr();
				prepositionIndex = Utils.lastIndexOfWord(request, preposition);
				endLocationPhraseIndex = prepositionIndex - 1;
			}
			
			String locationInput = request.substring(locationPhraseIndex, endLocationPhraseIndex);
			return parseLocationPhraseAndSetRelative(locationInput);
		}
		return null;
	}
	
	String parseLocationPhraseAndSetRelative(String locationInput){
		//TODO : if address is my location, get my location
		locationName = null;
		if(locationInput.equals("my location")){
			StringWrapper locationNameWrapper = new StringWrapper();
			Location location = vn.conghuy.talkwithme.android.utils.Utils.myLocation(myApp.locationManager, context, locationNameWrapper);
			if(location != null){
				locationName = locationNameWrapper.getStr();
				latitude = location.getLatitude();
				longtidude = location.getLongitude();
				showMap();
				return context.getString(R.string.show_map_command);
			}
		}
		
		DoubleWrapper latitudeWrapper = new DoubleWrapper();
		DoubleWrapper longtitudeWrapper = new DoubleWrapper();
		StringWrapper locationNameWrapper = new StringWrapper();
		vn.conghuy.talkwithme.android.utils.Utils.searchFromName(locationInput, context, latitudeWrapper, longtitudeWrapper, locationNameWrapper);
		locationName = locationNameWrapper.getStr();
		latitude = latitudeWrapper.getDouble();
		longtidude = longtitudeWrapper.getDouble();
		
		if(locationName != null){
			relativeWithLastResponse = RELATIVE_CONFIRM;
			mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_SHOW_LOCATION);
			return Utils.getRandomString(SqlData.mapAskLocationConfirm) + "\n" + locationName;
		}
		else{
			relativeWithLastResponse = RELATIVE_REINPUT_ADDRESS;
			mapResponsor.setRelativeLastResponse(MapResponsor.RELATIVE_SHOW_LOCATION);
			return Utils.getRandomString(SqlData.mapAskReinputAddress);
		}
	}
	
	void showMap(){
		String uriStr = "geo:0,0?q="+latitude+","+longtidude+" (" + locationName + ")";
		Intent intent = new Intent(android.content.Intent.ACTION_VIEW, 
				Uri.parse(uriStr));
		context.startActivity(intent);
	}
	
	public void reset(){
		locationName = null;
		latitude = 0;
		longtidude = 0;
	}
}
